const Controller = require('egg').Controller;

class NewsController extends Controller {
  /**
     *
     * @api {POST} /api/case/create 创建新闻
     * @apiName 创建新闻
     * @apiGroup 新闻模块
     * @apiVersion  1.0.0
     *
     *
     * @apiParam  {String} cover 新闻封面图
     * @apiParam  {String} title 新闻标题
     * @apiParam  {String} sketch 新闻描述
     * @apiParam  {String} content 新闻内容
     *
     *
     * @apiParamExample  {type} Request-Example:
        {
            "cover":"https://blog-1251395798.cos.ap-guangzhou.myqcloud.com/_1636424851757_.1.jpg",
            "title":"title",
            "sketch":"sketch",
            "content":"content"
        }
     *
     *
     * @apiSuccessExample {type} Success-Response:
        {
            "code": 200,
            "msg": "成功操作",
            "data": {
                "readCount": 0,    //新闻阅读次数
                "id": 3,        //新闻id
                "cover": "https://blog-1251395798.cos.ap-guangzhou.myqcloud.com/_1636424851757_.1.jpg", //新闻封面图
                "title": "title",   //新闻标题
                "sketch": "sketch", //新闻描述
                "content": "content",   //新闻内容
                "projectId": 1,
                "updatedAt": "2021-11-11T11:10:37.865Z",
                "createdAt": "2021-11-11T11:10:37.865Z"
            }
        }
     *
     *
     */
  async create() {
    const { app, ctx, service } = this;
    const checkRes = new ctx.helper.checkData(
      this,
      'cover',
      'title',
      'sketch',
      'content'
    ).checkDataRes;

    try {
      if (checkRes) {
        const res = await service.web.news.create();
        return ctx.helper.successModule(res);
      }
    } catch (error) {
      console.log(error);
      return ctx.helper.errorModule(error);
    }
  }

  /**
     *
     * @api {DELETE} /api/news/delete 删除新闻
     * @apiName 删除新闻
     * @apiGroup 新闻模块
     * @apiVersion  1.0.0
     *
     *
     * @apiParam  {Number} id 新闻id
     *
     *
     * @apiParamExample  {type} Request-Example:
        {
            "id":1
        }
     *
     *
     * @apiSuccessExample {type} Success-Response:
        {
            "code": 200,
            "msg": "删除成功",
            "data": []
        }
     *
     *
     */
  async delete() {
    const { app, ctx, service } = this;

    try {
      const res = await service.web.news.delete();
      if (res == 'id不存在,删除失败') {
        return ctx.helper.errorModule(res);
      }
      return ctx.helper.successModule(res);
    } catch (error) {
      console.log(error);
      return ctx.helper.errorModule(error);
    }
  }

  /**
     *
     * @api {PUT} /api/case/update 修改新闻
     * @apiName 修改新闻
     * @apiGroup 新闻模块
     * @apiVersion  1.0.0
     *
     * @apiParam  {String} cover 新闻封面图
     * @apiParam  {String} title 新闻标题
     * @apiParam  {String} sketch 新闻描述
     * @apiParam  {String} content 新闻内容
     *
     *
     * @apiParamExample  {type} Request-Example:
        {
            "id":3,
            "cover":"https://blog-1251395798.cos.ap-guangzhou.myqcloud.com/_1636424851757_.1.jpg",
            "title":"title",
            "sketch":"sketch",
            "content":"content"
        }
     *
     *
     * @apiSuccessExample {type} Success-Response:
        {
            "code": 200,
            "msg": "修改成功",
            "data": []
        }
     *
     *
     */
  async update() {
    const { app, ctx, service } = this;
    const checkRes = new ctx.helper.checkData(
      this,
      'id',
      'cover',
      'title',
      'sketch',
      'content'
    ).checkDataRes;

    try {
      if (checkRes) {
        const res = await service.web.news.update();
        if (res == 'id不存在,修改失败') {
          return ctx.helper.errorModule(res);
        }
        return ctx.helper.successModule(res);
      }
    } catch (error) {
      console.log(error);
      return ctx.helper.errorModule('操作失败');
    }
  }

  /**
     *
     * @api {GET} /api/news/getAll?limit=10&offset=0 获取所有新闻
     * @apiName 获取所有新闻
     * @apiGroup 新闻模块
     * @apiVersion  1.0.0
     *
     *
     * @apiParam  {Number} limit 新闻返回数
     * @apiParam  {Number} offset 新闻偏移数
     *
     *
     * @apiSuccessExample {type} Success-Response:
        {
            "code": 200,
            "msg": "成功操作",
            "data": {
                "count": 1,     //新闻总数
                "rows": [
                    {
                        "id": 2,    //新闻id
                        "cover": "https://blog-1251395798.cos.ap-guangzhou.myqcloud.com/_1636424851757_.1.jpg",//新闻封面图
                        "title": "title",   //新闻标题
                        "sketch": "sketch", //新闻描述
                        "content": "content",   //新闻内容
                        "readCount": 2, //新闻阅读次数
                        "projectId": 1, //新闻所属项目id
                        "createdAt": "2021-11-11T06:57:26.000Z",    //新闻发布时间
                        "updatedAt": "2021-11-11T11:15:47.000Z"
                    }
                ]
            }
        }
     *
     *
     */
  async getAll() {
    const { app, ctx, service } = this;
    try {
      const res = await service.web.news.getAll();
      return ctx.helper.successModule(res);
    } catch (error) {
      console.log(error);
      return ctx.helper.errorModule('操作失败');
    }
  }

  /**
     *
     * @api {GET} /api/news/getById?id=2 根据id查找新闻
     * @apiName 根据id查找新闻
     * @apiGroup 新闻模块
     * @apiVersion  1.0.0
     *
     *
     * @apiParam  {Number} id 新闻id
     *
     *
     *
     *
     * @apiSuccessExample {type} Success-Response:
        {
            "code": 200,
            "msg": "成功操作",
            "data": {
                "id": 2,    //新闻id
                "cover": "https://blog-1251395798.cos.ap-guangzhou.myqcloud.com/_1636424851757_.1.jpg", //新闻封面图
                "title": "title",   //新闻标题
                "sketch": "sketch", //新闻描述
                "content": "content",   //新闻内容
                "readCount": 2, //新闻阅读次数
                "projectId": 1, //所属项目id
                "createdAt": "2021-11-11T06:57:26.000Z",    //发布时间
                "updatedAt": "2021-11-11T11:15:47.000Z"
            }
        }
     *
     *
     */
  async getById() {
    const { app, ctx, service } = this;
    const checkRes = new ctx.helper.checkData(this, 'id').checkDataRes;
    try {
      if (checkRes) {
        const res = await service.web.news.getById();
        return ctx.helper.successModule(res);
      }
    } catch (error) {
      return ctx.helper.errorModule(error);
    }
  }
}
module.exports = NewsController;
